package com.xhwl.logistics.converter.mapstruct;

import com.google.common.base.Strings;
import com.xhwl.logistics.bo.cent.ProjectBO;
import com.xhwl.logistics.dto.push.CarInHuaWeiIotDTO;
import com.xhwl.logistics.dto.push.CarInKeTopDTO;
import com.xhwl.logistics.dto.push.CarOutHuaWeiIotDTO;
import com.xhwl.logistics.dto.push.CarOutKeTopDTO;
import com.xhwl.logistics.dto.push.CarPayKeTopDTO;
import com.xhwl.logistics.entity.VehicleAccessRecord;
import com.xhwl.logistics.entity.VehiclePayRecord;
import com.xhwl.logistics.enums.VehicleCardTypeEnum;
import com.xhwl.logistics.enums.VehiclePassTypeEnum;
import org.apache.commons.lang3.RegExUtils;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.NullValueMappingStrategy;

import java.math.BigDecimal;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

@Mapper(componentModel = "spring", nullValueMappingStrategy = NullValueMappingStrategy.RETURN_DEFAULT)
public interface VehicleDataPushConverter {

    BigDecimal vehiclePayBigDecimal = new BigDecimal(100);

    //*********Data Push HuaWeiIot*********
    default List<VehicleAccessRecord> toVehicleAccessRecordList(List<CarInHuaWeiIotDTO> carInHuaWeiIotDTOList, ProjectBO projectBO) {
        return carInHuaWeiIotDTOList.stream()
                .map(carInHuaWeiIotDTO -> toVehicleAccessRecord(carInHuaWeiIotDTO, projectBO))
                .collect(Collectors.toList());
    }
    @Mapping(target = "parkId", source = "carInHuaWeiIotDTO.carinParkId")
    @Mapping(target = "plateNumber", source = "carInHuaWeiIotDTO.carinPlateNumber")
    @Mapping(target = "enterTime", source = "carInHuaWeiIotDTO.carinInTime", dateFormat = "yyyy-MM-dd HH:mm:ss")
    @Mapping(target = "enterPictureUrl", source = "carInHuaWeiIotDTO.carinInImage")
    @Mapping(target = "recordId", source = "carInHuaWeiIotDTO.carinInRecordId")
    @Mapping(target = "status", constant = "0")
    VehicleAccessRecord toVehicleAccessRecord(CarInHuaWeiIotDTO carInHuaWeiIotDTO, ProjectBO projectBO);

    @Mapping(target = "parkId", source = "carOutHuaWeiIotDTO.caroutParkId")
    @Mapping(target = "plateNumber", source = "carOutHuaWeiIotDTO.caroutPlateNumber")
    @Mapping(target = "exitTime", source = "carOutHuaWeiIotDTO.caroutOutTime", dateFormat = "yyyy-MM-dd HH:mm:ss")
    @Mapping(target = "exitPictureUrl", source = "carOutHuaWeiIotDTO.caroutOutImage")
    @Mapping(target = "recordId", source = "carOutHuaWeiIotDTO.caroutInRecordId")
    @Mapping(target = "status", constant = "1")
    VehicleAccessRecord toVehicleAccessRecord(CarOutHuaWeiIotDTO carOutHuaWeiIotDTO, ProjectBO projectBO);

    @Mapping(target = "exitTime", source = "carOutHuaWeiIotDTO.caroutOutTime", dateFormat = "yyyy-MM-dd HH:mm:ss")
    @Mapping(target = "exitPictureUrl", source = "carOutHuaWeiIotDTO.caroutOutImage")
    @Mapping(target = "status", constant = "1")
    VehicleAccessRecord toVehicleAccessRecord(CarOutHuaWeiIotDTO carOutHuaWeiIotDTO, Integer id);

    default VehiclePayRecord toVehiclePayRecordHuaWeiIot(CarOutHuaWeiIotDTO carOutHuaWeiIotDTO, Integer projectId) {
        VehiclePayRecord vehiclePayRecord = toVehiclePayRecord(carOutHuaWeiIotDTO, projectId);

        String totalMoney = carOutHuaWeiIotDTO.getCaroutChargeTotal();
        if (Strings.isNullOrEmpty(totalMoney)) {
            vehiclePayRecord.setTotalMoney(0);
        } else {
            vehiclePayRecord.setTotalMoney(new BigDecimal(totalMoney).multiply(vehiclePayBigDecimal).intValue());
        }

        String freeMoney = carOutHuaWeiIotDTO.getCaroutDiscountAmount();
        if (Strings.isNullOrEmpty(freeMoney)) {
            vehiclePayRecord.setFreeMoney(0);
        } else {
            vehiclePayRecord.setFreeMoney(new BigDecimal(freeMoney).multiply(vehiclePayBigDecimal).intValue());
        }

        String paidMoney = carOutHuaWeiIotDTO.getCaroutCharge();
        if (Strings.isNullOrEmpty(paidMoney)) {
            vehiclePayRecord.setPaidMoney(0);
        } else {
            vehiclePayRecord.setPaidMoney(new BigDecimal(paidMoney).multiply(vehiclePayBigDecimal).intValue());
        }

        return vehiclePayRecord;
    }
    @Mapping(target = "parkId", source = "carOutHuaWeiIotDTO.caroutParkId")
    @Mapping(target = "plateNumber", source = "carOutHuaWeiIotDTO.caroutPlateNumber")
    @Mapping(target = "accessRecordId", source = "carOutHuaWeiIotDTO.caroutInRecordId")
    @Mapping(target = "enterTime", source = "carOutHuaWeiIotDTO.caroutInTime", dateFormat = "yyyy-MM-dd HH:mm:ss")
    @Mapping(target = "payTime", source = "carOutHuaWeiIotDTO.caroutOutTime", dateFormat = "yyyy-MM-dd HH:mm:ss")
    VehiclePayRecord toVehiclePayRecord(CarOutHuaWeiIotDTO carOutHuaWeiIotDTO, Integer projectId);

    //*********Data Push HuaWeiIot*********



    //*********Data Push KeTop*********
    String passageNameRegex = "-.*";
    String irregularPassageNameAfterRegex = "其他";

    static String exitPassageNameAfterRegex(CarOutKeTopDTO carOutKeTopDTO) {
        String leavePlace = carOutKeTopDTO.getLeavePlace();
        String exitPassageNameAfterRegex = RegExUtils.removeAll(leavePlace, passageNameRegex);
        if (Strings.isNullOrEmpty(exitPassageNameAfterRegex)) {
            return irregularPassageNameAfterRegex;
        } else {
            return exitPassageNameAfterRegex;
        }
    }

    default VehicleAccessRecord toVehicleAccessRecordByKeTop(CarInKeTopDTO carInKeTopDTO, ProjectBO projectBO) {
        VehicleAccessRecord vehicleAccessRecord = toVehicleAccessRecord(carInKeTopDTO, projectBO);

        String plateNo = carInKeTopDTO.getPlateNo();
        vehicleAccessRecord.setPlateNumber(plateNo);

        String entryPlace = carInKeTopDTO.getEntryPlace();
        String enterPassageNameAfterRegex = RegExUtils.removeAll(entryPlace, passageNameRegex);
        if (Strings.isNullOrEmpty(enterPassageNameAfterRegex)) {
            vehicleAccessRecord.setEnterPassageName(irregularPassageNameAfterRegex);
        } else {
            vehicleAccessRecord.setEnterPassageName(enterPassageNameAfterRegex);
        }

        Integer carType = carInKeTopDTO.getCarType();
        if (Objects.nonNull(carType)) {
            vehicleAccessRecord.setCardType(VehicleCardTypeEnum.toVehicleCardTypeEnumByKeTop(carType).getId());
        }

        Integer passType = carInKeTopDTO.getPassType();
        if (Objects.nonNull(passType)) {
            vehicleAccessRecord.setEnterType(VehiclePassTypeEnum.toVehiclePassTypeEnumByKeTop(passType).getId());
        }

        return vehicleAccessRecord;
    }
    @Mapping(target = "enterTime", source = "carInKeTopDTO.entryTime", dateFormat = "yyyy-MM-dd HH:mm:ss")
    @Mapping(target = "enterPictureUrl", source = "carInKeTopDTO.imgInfo")
//    @Mapping(target = "enterPassageName", source = "carInKeTopDTO.entryPlace")
    @Mapping(target = "recordId", source = "carInKeTopDTO.trafficId")
    @Mapping(target = "status", constant = "0")
    VehicleAccessRecord toVehicleAccessRecord(CarInKeTopDTO carInKeTopDTO, ProjectBO projectBO);

    default VehicleAccessRecord toVehicleAccessRecordByKeTop(CarOutKeTopDTO carOutKeTopDTO, ProjectBO projectBO) {
        VehicleAccessRecord vehicleAccessRecord = toVehicleAccessRecord(carOutKeTopDTO, projectBO);

        String plateNo = carOutKeTopDTO.getPlateNo();
        vehicleAccessRecord.setPlateNumber(plateNo);

        vehicleAccessRecord.setExitPassageName(exitPassageNameAfterRegex(carOutKeTopDTO));

        Integer carType = carOutKeTopDTO.getCarType();
        if (Objects.nonNull(carType)) {
            vehicleAccessRecord.setCardType(VehicleCardTypeEnum.toVehicleCardTypeEnumByKeTop(carType).getId());
        }

        Integer passType = carOutKeTopDTO.getPassType();
        if (Objects.nonNull(passType)) {
            vehicleAccessRecord.setExitType(VehiclePassTypeEnum.toVehiclePassTypeEnumByKeTop(passType).getId());
        }

        return vehicleAccessRecord;
    }
    @Mapping(target = "exitTime", source = "carOutKeTopDTO.leaveTime", dateFormat = "yyyy-MM-dd HH:mm:ss")
    @Mapping(target = "exitPictureUrl", source = "carOutKeTopDTO.imgInfo")
//    @Mapping(target = "exitPassageName", source = "carOutKeTopDTO.leavePlace")
    @Mapping(target = "recordId", source = "carOutKeTopDTO.trafficId")
    @Mapping(target = "status", constant = "1")
    VehicleAccessRecord toVehicleAccessRecord(CarOutKeTopDTO carOutKeTopDTO, ProjectBO projectBO);

    default VehicleAccessRecord toVehicleAccessRecordByKeTop(CarOutKeTopDTO carOutKeTopDTO, Integer id) {
        VehicleAccessRecord vehicleAccessRecord = toVehicleAccessRecord(carOutKeTopDTO, id);

        vehicleAccessRecord.setExitPassageName(exitPassageNameAfterRegex(carOutKeTopDTO));

        Integer passType = carOutKeTopDTO.getPassType();
        if (Objects.nonNull(passType)) {
            vehicleAccessRecord.setExitType(VehiclePassTypeEnum.toVehiclePassTypeEnumByKeTop(passType).getId());
        }

        return vehicleAccessRecord;
    }
    @Mapping(target = "exitTime", source = "carOutKeTopDTO.leaveTime", dateFormat = "yyyy-MM-dd HH:mm:ss")
    @Mapping(target = "exitPictureUrl", source = "carOutKeTopDTO.imgInfo")
//    @Mapping(target = "exitPassageName", source = "carOutKeTopDTO.leavePlace")
    @Mapping(target = "status", constant = "1")
    VehicleAccessRecord toVehicleAccessRecord(CarOutKeTopDTO carOutKeTopDTO, Integer id);

    @Mapping(target = "plateNumber", source = "carPayKeTopDTO.plateNo")
    @Mapping(target = "accessRecordId", source = "carPayKeTopDTO.billId")
    @Mapping(target = "payRecordId", source = "carPayKeTopDTO.orderNo")
    @Mapping(target = "enterTime", source = "carPayKeTopDTO.entryTime", dateFormat = "yyyy-MM-dd HH:mm:ss")
    @Mapping(target = "payTime", source = "carPayKeTopDTO.payTime", dateFormat = "yyyy-MM-dd HH:mm:ss")
    VehiclePayRecord toVehiclePayRecord(CarPayKeTopDTO carPayKeTopDTO, Integer projectId);

    //*********Data Push KeTop*********

}
